package com.zyx.vo;

import com.zyx.pojo.Admin;
import com.zyx.pojo.Spencer;
import com.zyx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
  * @author zyx
  * @date 2022/1/11
  */

@Controller
public class GetSession {

    /**
     * 根据cookie获取管理员信息
     * @return
     */
    public Admin adminSession(HttpServletRequest request,HttpServletResponse response,HttpSession session){
        Admin admin = new Admin();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if("admin".equals(cookie.getName())){
                admin = (Admin)session.getAttribute("admin"+cookie.getValue());
            }
        }
        return admin;
    }

    /**
     * 根据cookie获取店铺信息
     * @return
     */
    public Spencer spencerSession(HttpServletRequest request,HttpServletResponse response,HttpSession session){
        Spencer spencer = new Spencer();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if("spencer".equals(cookie.getName())){
                spencer = (Spencer)session.getAttribute("spencer"+cookie.getValue());
            }
        }
        return spencer;
    }

    /**
     * 根据cookie获取用户信息
     * @return
     */
    public User userSession(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        User user = new User();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if("user".equals(cookie.getName())){
                user = (User) session.getAttribute("user" + cookie.getValue());
            }
        }
        return user;
    }
}
